package com.aistar.controller;


import com.aistar.pojo.CodeStatus;
import com.aistar.pojo.Member;
import com.aistar.pojo.UserResult;
import com.aistar.service.LoginService;
import com.aistar.service.MemberService;
import com.aistar.util.Config;

import com.aistar.util.MD5Utils;
import net.bytebuddy.asm.Advice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/login")
public class LoginController extends HttpServlet {
    @Autowired
    private LoginService loginService;
    @Autowired
    private MemberService memberService;

    @RequestMapping("/login")
    public String returnLogin() {
        return "/RegistLogin/login";
    }
    //退出
    @RequestMapping("/logout")
    public String logout(HttpServletRequest req){
        HttpSession session = req.getSession();
        session.invalidate();
        return "index";
    }
    //账号密码登录
    @PostMapping("/normalLogin")
    public String normalLogin(Member member, HttpServletRequest req) {
        System.out.println(member);
        HttpSession session = req.getSession();
        System.out.println("cjvkhjc"+member.getUserAccount()+member.getUserPassword());

        String mimacc = member.getUserPassword();

    /*    Member member1 = loginService.findByAccount(member.getUserAccount(),member.getUserPassword());*/
        Member member1 = memberService.getbyACC(member.getUserAccount());
        System.out.println("mima为" + member1.getUserPassword());
        System.out.println("密码" +member.getUserPassword() );
        System.out.println("????????"+(member1.getUserPassword().equals(member.getUserPassword())));

        System.out.println("988"+member1);
        /*密码解密*/
        String WebPassword = MD5Utils.md5(member.getUserPassword());
        if (!(member1.getUserPassword().equals(WebPassword))) {
            return "redirect:/RegistLogin/login.jsp?error=0";
        }
        session.setAttribute("userId", member1.getUserId());
        UserResult userResult = new UserResult(CodeStatus.ERROR, true, "");
        return "redirect:/start/index";

    }
    //手机验证码登陆
    @PostMapping("/getcode")
    public void getSsmCode(HttpServletRequest request, HttpServletResponse response) throws IOException { //1. 验证手机号码格式
        String userTel = request.getParameter("userTel");
        System.out.println("to = " + userTel);
        String code = Config.getCode(userTel);
        System.out.println("code:"+code);
        response.getWriter().print(code);//将数据返回到前端


    }
}
